<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单业务" prop="appointmentBusiness">
        <el-select v-model="queryParams.appointmentBusiness" placeholder="请选择订单业务" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_appointment_business"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="维修组号" align="center" prop="groupId" />
      <el-table-column label="预约单号" align="center" prop="appointmentId" />
      <el-table-column label="订单业务" align="center" prop="appointmentBusiness">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_appointment_business" :value="scope.row.appointmentBusiness"/>
        </template>
      </el-table-column>
      <el-table-column label="维修问题" align="center" prop="orderProblem" />
      <el-table-column label="维修措施" align="center" prop="orderMeasure" />
      <el-table-column label="完成工时" align="center" prop="orderTime" />
      <el-table-column label="完成时间" align="center" prop="orderEndtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderEndtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单费用" align="center" prop="orderCost" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleUpdate(scope.row)"
          >详情</el-button>
          <el-button
            v-if="scope.row.orderState == 5"
            size="mini"
            type="text"
            icon="el-icon-star-off"
            @click="handleAdd(scope.row)"
            v-hasPermi="['bysj:order2:add']"
          >评价</el-button>
          <el-button
            v-if="scope.row.orderState ==3"
            size="mini"
            type="text"
            icon="el-icon-goods"
            @click="handlePay(scope.row)"
            v-hasPermi="['bysj:order2:pay']"
          >支付</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open1" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单状态" prop="orderState">
          <el-steps :active="form.orderState" finish-status="success">
            <el-step title="预约成功"></el-step>
            <el-step title="接车成功"></el-step>
            <el-step title="维修完成"></el-step>
            <el-step title="支付成功"></el-step>
            <el-step title="取车成功"></el-step>
            <el-step title="评价成功"></el-step>
          </el-steps>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="订单编号" prop="orderId">
              <el-input v-model="form.orderId" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="维修组号" prop="groupId">
              <el-input v-model="form.groupId" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单业务" prop="appointmentBusiness">
              <el-select v-model="form.appointmentBusiness" :disabled="true">
                <el-option
                  v-for="dict in dict.type.sys_appointment_business"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="车主" prop="appointmentName">
              <el-input v-model="form.appointmentName" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆型号" prop="appointmentCar">
              <el-input v-model="form.appointmentCar" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车牌" prop="carId">
              <el-input v-model="form.carId" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="维修问题" prop="orderProblem">
          <el-input v-model="form.orderProblem" type="textarea" :disabled="true"/>
        </el-form-item>
        <el-form-item label="维修措施" prop="orderMeasure">
          <el-input v-model="form.orderMeasure" type="textarea" :disabled="true"/>
        </el-form-item>
        <el-row>
          <el-col :span="8">
            <el-form-item label="完成工时" prop="orderTime">
              <el-input v-model="form.orderTime" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="完成时间" prop="orderEndtime">
              <el-date-picker clearable size="small"
                              v-model="form.orderEndtime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              :disabled="true">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="人工费用" prop="orderTime">
              <el-input v-model="form.artificialCost" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="配件费用" prop="orderEndtime">
              <el-input v-model="form.goodsCost" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单费用" prop="orderCost">
              <el-input v-model="form.orderCost" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="评分" prop="evaluationScore">
          <el-rate v-model="form.evaluationScore" placeholder="请输入评分" show-text />
        </el-form-item>
        <el-form-item label="评价内容" prop="evaluationContent">
          <el-input v-model="form.evaluationContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open2" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="" />
        </el-form-item>
        <el-form-item label="车主" prop="appointmentName">
          <el-input v-model="form.appointmentName" placeholder="" />
        </el-form-item>
        <el-form-item label="完成工时" prop="orderTime">
          <el-input v-model="form.orderTime" placeholder="" />
        </el-form-item>
        <el-form-item label="人工费用" prop="artificialCost">
          <el-input v-model="form.artificialCost" placeholder="" />
        </el-form-item>
        <el-form-item label="所需配件" prop="orderGoods">
          <el-input v-model="form.orderGoods" placeholder="" type="textarea" />
        </el-form-item>
        <el-form-item label="配件费用" prop="goodsCost">
          <el-input v-model="form.goodsCost" placeholder="" />
        </el-form-item>
        <el-form-item label="订单费用" prop="orderCost">
          <el-input v-model="form.orderCost" placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">支付</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listOrder, getOrder, updateOrder,updatePay } from "@/api/bysj/order2";
import {  addEvaluation } from "@/api/bysj/evaluation";

export default {
  name: "Order",
  dicts: ['sys_appointment_business'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      open1: false,
      open2: false,
      open3: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        appointmentId: null,
        appointmentBusiness: null,
        appointmentName: null,
        appointmentCar: null,
        appointmentProblem: null,
        orderReceiver: null,
        orderProblem: null,
        orderMeasure: null,
        orderTime: null,
        orderEndtime: null,
        orderCost: null,
        orderName: null,
        evaluationScore: null,
        evaluationContent: null,
        artificialCost: null,
        orderGoods: null,
        goodsCost: null,
        orderState: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel1() {
      this.open2 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        groupId: null,
        appointmentId: null,
        appointmentBusiness: null,
        appointmentName: null,
        appointmentCar: null,
        appointmentProblem: null,
        orderReceiver: null,
        orderProblem: null,
        orderMeasure: null,
        orderTime: null,
        orderEndtime: null,
        orderCost: null,
        orderName: null,
        evaluationScore: null,
        evaluationContent: null,
        artificialCost: null,
        orderGoods: null,
        goodsCost: null,
        orderState: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.orderId = row.orderId || this.ids;
      this.open = true;
      this.title = "评价";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "订单详细";
      });
    },
    handlePay(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "支付费用";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.evaluationScore != null) {
            addEvaluation(this.form).then(response => {
              this.$modal.msgSuccess("评价成功");
              this.open = false;
              this.getList();
            });

          }
          else if (this.form.appointmentName != null) {
            updatePay(this.form).then(response => {
              this.$modal.msgSuccess("支付成功");
              this.open2 = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>

